//
//  TYZCoreInfo.m
//  TYZ_SC
//
//  Created by 李贤立 on 2017/12/12.
//  Copyright © 2017年 Capfer. All rights reserved.
//

#import "TYZCoreInfo.h"
#import "TYZLanguageTool.h"
@implementation TYZCoreInfo
TYZSingleto_m(CoreInfo)
/**
 *  初始化基本信息
 */
+ (void)initial {
    // 计算屏幕宽高
    BaseInfo.screenWidth = [UIScreen mainScreen].bounds.size.width;
    BaseInfo.screenHeight = [UIScreen mainScreen].bounds.size.height;
    // 适配宽高和字体的倍数
    BaseInfo.widthSuited = BaseInfo.screenWidth / 375;
    BaseInfo.heightSuited = BaseInfo.screenHeight / 667;
    BaseInfo.fontSuited = hypot(BaseInfo.screenWidth, BaseInfo.screenHeight) / hypot(375, 667);
    // 获取版本号
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    CoreInfo.app_version = infoDictionary[@"CFBundleShortVersionString"];
    // 判断设备类型
    CoreInfo.DeviceType = [TYZCoreInfo getDeviceType];
    // 获取用户ID
    CoreInfo.user_id = [[NSUserDefaults standardUserDefaults] objectForKey:Symbol_User_Id];
    // 设置语言和字体
    [TYZLanguageTool configLanguageAndFonSize];
    // 获取登录状态
    CoreInfo.rememberMe = [[NSUserDefaults standardUserDefaults] objectForKey:Symbol_RememberMe];
    CoreInfo.session_id = [[NSUserDefaults standardUserDefaults] objectForKey:Symbol_Session_Id];
    [NetTool setupCookie];
    // 设置通知数量
    CoreInfo.notifacationNumber ++;
}
+ (TYZDeviceType)getDeviceType {
    switch ([UIDevice currentDevice].userInterfaceIdiom) {
        case UIUserInterfaceIdiomPhone:
            return DEVICE_TYPE_IPHONE;
            break;
        case UIUserInterfaceIdiomPad:
            return DEVICE_TYPE_IPAD;
            break;
        case UIUserInterfaceIdiomCarPlay:
            return DEVICE_TYPE_CARPLAY;
            break;
        case UIUserInterfaceIdiomUnspecified:
            return DEVICE_TYPE_OTHER;
            break;
        default:
            return DEVICE_TYPE_OTHER;
            break;
    }
}
/**
 *  设置程序图标显示的数目
 */
+ (void)setAppBadgeNumber:(NSInteger)AppBadgeNumber {
    // 进行注册
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [UIApplication sharedApplication].applicationIconBadgeNumber = AppBadgeNumber;
}
- (void)setNotifacationNumber:(NSInteger)notifacationNumber {
    _notifacationNumber = notifacationNumber;
    [TYZCoreInfo setAppBadgeNumber:notifacationNumber];
}
- (KHJUserInfo *)userInfo {
    if (_userInfo == nil) {
        _userInfo = [[KHJUserInfo alloc] init];
    }
    return _userInfo;
}
- (void)setSession_id:(NSString *)session_id {
    _session_id = session_id;
    [[NSUserDefaults standardUserDefaults] setObject:session_id forKey:Symbol_Session_Id];
}
- (void)setRememberMe:(NSString *)rememberMe {
    _rememberMe = rememberMe;
    [[NSUserDefaults standardUserDefaults] setObject:rememberMe forKey:Symbol_RememberMe];
}

/**
 *  跳转到设置界面
 *
 *  @param pushSettingType 设置界面的类型
 */
+ (void)pushToDeviceSettingWithPushSettingType:(TYZPushSettingType)pushSettingType {
    NSString *urlString = BaseInfo.settingTypeArray[pushSettingType];
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlString]]) {
        if (@available(iOS 10.0, *)) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:nil];
        } else {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        }
    }
}
@end
